package cn.hd.commonService.vo;


import cn.hd.commonService.exception.CustomException;
import lombok.Data;


@Data
public class Result<T>  {

    //状态码
    private Integer code;
    //提示信息
    private String msg;
    //数据
    private T data;

    public Result() {

    }

    public Result(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Result(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static Result error(String msg) {
        Result result = new Result<>();
        result.setCode(500);
        result.setData(null);
        result.setMsg(msg);
        return result;
    }

    //处理返回成功的结果
    public static Result success(String msg){
        Result result = new Result();
        result.setCode(200);
        result.setMsg(msg);
        result.setData(null);
        return result;
    }
    //处理返回成功的结果
    public static Result success(String msg,Object data){
        Result result = new Result();
        result.setCode(200);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }
    //自定义异常返回的结果
    public static Result defineError(CustomException e){
        Result result = new Result();
        result.setCode(e.getErrorCode());
        result.setMsg(e.getErrorMsg());
        result.setData(null);
        return result;
    }

}
